package com.shishuo.cms.action.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.ArticleEntity;
import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.PictureEntity;
import com.shishuo.cms.entity.VideoEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.VideoService;

@Controller
@RequestMapping("/admin/video")
public class VideoAction extends AdminBaseAction{
	@InitBinder("videoEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("videoEntity.");
	}

	@Autowired
	private VideoService videoService;

	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addvideoEntity(ModelMap modelMap) throws Exception {
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("video");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/video/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addVideo(VideoEntity videoEntity,
			@RequestParam(value = "content", defaultValue = "") String content) throws Exception{
		videoEntity.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (videoEntity.getName() == null
				|| "".equals(videoEntity.getName())) {
			json.getErrors().put("name", "名称不能为空");
		}
		if (videoEntity.getThumbnail() == null
				|| "".equals(videoEntity.getThumbnail())) {
			json.getErrors().put("thumbnail", "缩略图不能为空");
		}
		if (videoEntity.getFlashUrl() == null
				|| "".equals(videoEntity.getFlashUrl())) {
			json.getErrors().put("flashUrl", "视频地址不能为空");
		}
		if (videoEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		videoService.insert(videoEntity);
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", videoService.getAllListPage(pageNum));
		return "system/video/all";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> delete(
			@RequestParam(value = "id") long id) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		videoService.deleteById(id);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(@RequestParam(value = "id") long id,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		VideoEntity videoEntity=videoService.getById(id);
		modelMap.put("videoEntity", videoEntity);
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("video");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/video/update";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> update(
			@ModelAttribute VideoEntity videoEntity,
			@RequestParam(value = "content", defaultValue = "") String content) throws Exception {
		videoEntity.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (videoEntity.getName() == null
				|| "".equals(videoEntity.getName())) {
			json.getErrors().put("name", "名称不能为空");
		}
		if (videoEntity.getFlashUrl() == null
				|| "".equals(videoEntity.getFlashUrl())) {
			json.getErrors().put("flashUrl", "视频地址不能为空");
		}
		if (videoEntity.getThumbnail() == null
				|| "".equals(videoEntity.getThumbnail())) {
			json.getErrors().put("thumbnail", "缩略图不能为空");
		}
		if (videoEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		VideoEntity oldVideoEntity=videoService.getById(videoEntity.getId());
		if (null==oldVideoEntity) {
			json.getErrors().put("name", "不存在");
			return json;
		}
		oldVideoEntity.setClassId(videoEntity.getClassId());
		oldVideoEntity.setContent(content);
		oldVideoEntity.setThumbnail(videoEntity.getThumbnail());
		oldVideoEntity.setLastUpdateDate(new Date());
		oldVideoEntity.setName(videoEntity.getName());
		videoService.update(oldVideoEntity);
		json.setResult(true);
		return json;
	}
}
